﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using BussinesLogic;
using System.Collections;

namespace DataAccess.Factory
{
    class UsuarioFactoryDao : BaseFactory
    {

        public UsuarioFactoryDao()
        {
            this.classType = typeof(Usuario);
        }

        private string uspGetUserByUsernameAndPassword = @"fd_Usuario_get_by_username_and_password";
        private string uspGetAllUsers = @"fd_Usuarios_All";
        private string uspCreateUser = @"fd_Usuario_Create";

        /* funciones */

        public int createUser(string xmlUser)
        {
            SqlParameter[] parameter = new SqlParameter[2];

            parameter[0] = new SqlParameter("@usuario", xmlUser);
            parameter[1] = new SqlParameter("@ID", SqlDbType.Int);
            parameter[1].Direction = ParameterDirection.Output;

            this.executeNoQuery(parameter, this.uspCreateUser);

            return (int)parameter[1].Value;
        }

        public ArrayList getAllUsers()
        {
            return this.DataTableToObjectList(null, this.uspGetAllUsers);
        }

        public Usuario getUserByUserNameAndPassword(string username, string password)
        {
            SqlParameter [] parameter = new SqlParameter[2];

            parameter[0] = new SqlParameter("@username", username);
            parameter[1] = new SqlParameter("@password", password);

            ArrayList obj = this.DataTableToObjectList(parameter, this.uspGetUserByUsernameAndPassword);

            if (obj != null && obj.Count > 0)
            {
                return (Usuario)obj[0];
            }
            else
            {
                return null;
            }
            
        }
    }
}
